/** @file edit.cpp
 */
#include "edit.h"
#include "string.h"
#include "stdio.h"
#include "stdlib.h"

Edit::Edit(HWND parent, char *txt):Widget(parent)
{
    name ="Edit";
    activable =1;

    font = font_STANDARD;
    digitMode =0;

    keyShift =0;
    setText(txt);

}

Edit::~Edit()
{
    cleanWidgets();
}

/** @function setLen, set Edit digit length
 *            and the characters are ' 's 
 */
void Edit::setLen(char len)
{
    int i;
    
    if(len >= sizeof(text) ) return;
    
    for(i=0; i<len; i++)
    {
        if(digitMode)
            text[i] ='0';
        else
            text[i] =' ';
    }
    text[i] =0;
    
    setText( text);
}

void Edit::setFont(FONT *font)
{
    this->font =font;
}

bool Edit::isDigit()
{
    return digitMode;
}

void Edit::setDigitMode(bool mode)
{
    digitMode =mode;
    setLen(strlen(text));
}

char* Edit::getText()
{
    return text;
}

void Edit::setText(char *c)
{
    int i;
    HWND hWnd;
    int stringLen;
    
    stringLen =strlen(c);
    stringLen =stringLen< sizeof(text)-1? stringLen: sizeof(text);
    
    for(i=0; i<stringLen; i++)
    {
        if(*c ==0)
        {
            text[i] =0;
        }
        else
        {
            if(digitMode)
            {
                text[i] = *c>='0'? *c: '0';
                text[i] = text[i] <='9'? text[i]: '9';
            }
            else
            {
                text[i] =*c>' '? *c: ' ';
                text[i] =*c<'z'? *c: 'z';
            }
            c++;
        }
    }

    text[i] =0;
    stringLen =strlen(text);
    
    //delete older characters
    while(hWnd =getFirstItem(&childList))
    {
        removeChild(hWnd);
        delete hWnd;
    }

    //first set this size;
    //then set child's size
   setSize(2 + stringLen*(font->width+1), 2+font->height);

    for(i=0; i<stringLen; i++)
    {
        char *name;
        Letter *pLetter;
        pLetter =new Letter(this);

        pLetter ->name = "Letter";

        pLetter->setMode(this->digitMode);
        pLetter->setFont(this->font);
        pLetter->setChar(text+i);
        pLetter->setPosition(i*(pLetter->sizeX()) +1,1);

        connect(&pLetter->signal[SIGNAL_LETTER_CHANGED], this);
    }

    //sendMessage(this, MSG_PAINT, 0,0);
}

void Edit::draw(Rect *r)
{
    if(isActive())
    {
        pDC->drawRect(rect.x0,rect.y0, rect.x1,rect.y1, 0x0F);
    }else
    {
        pDC->drawRect(rect.x0,rect.y0, rect.x1,rect.y1, 0x00);
    }

    pDC->dirty(max(r->x0,rect.x0), max(r->y0,rect.y0), min(r->x1,rect.x1), min(r->y1,rect.y1));
    Widget::draw(r);
}


int Edit::eventHandler(HWND hWnd, int iMsg, WPARAM wParam, LPARAM lParam)
{
    /// signal handling
    switch(iMsg)
    {
        case MSG_SIGNAL:
            {
                Letter *hwnd;
                
                for(hwnd=(Letter *)getFirstItem(&childList); hwnd; hwnd =(Letter *)hwnd->next)
                {
                    if(SIGNAL_MATCH(wParam, &hwnd->signal[SIGNAL_LETTER_CHANGED]) )
                    {
                        signal[SIGNAL_EDIT_CHANGED].emitSignal();
                        return 0;
                    }
                }
            }
            break;
    }

    /// relations handling
    switch(iMsg)
    {
    case MSG_ACTIVE_ELDER_CHILD:
        activePrevChild();
        break;
        
    case MSG_ACTIVE_YOUNGER_CHILD:
        activeNextChild();
        break;
        
    case MSG_KEY_DOWN:
        //Child have the Active status
        if(activeChild)
        {
            sendMessage( activeChild, iMsg, wParam, lParam);
        }
        
        else
        {
            switch(wParam)
            {
            case KEY_ENTER:
                activeFirstChild();
                break;
            }
            Widget::eventHandler(hWnd, iMsg, wParam, lParam);
            
        }
        break;
        
    case MSG_KEY_UP:
        if(activeChild)
        {
            sendMessage( activeChild, iMsg, wParam, lParam);
        }
        break;

        
        //edit ended
    case MSG_LEVEL_UP:
        asm("nop\n");
        break;
    }
    
    return 0;
}

void Edit::leaveFocus()
{
    signal[SIGNAL_EDIT_LEAVE_FOCUS].emitSignal();
}

void Edit::setupWidgets()
{
}

void Edit::cleanWidgets()
{
    HWND hWnd;
    //delete older characters
    while(hWnd =getFirstItem(&childList))
    {
        removeChild(hWnd);
        delete hWnd;
    }
}
